<template>
  <div class="shop">
    <div class="wap-content">
      <div class="query-wrapper">
        <div class="query-item">
          <div>店铺名称</div>
          <el-input
            v-model="params.storeName"
            placeholder="请输入店铺名称"
            icon="el-icon-search"
            clearable
            style="width: 150px"
            @on-clear="shopsData=[]; params.storeName=''; params.current =1; init()"
            @on-enter="()=>{shopsData=[]; params.current =1; init();}"
          />
        </div>

        <div class="query-item">
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="shopsData=[];params.current =1; init();"
          >搜索</el-button>
        </div>
      </div>
      <div>
        <el-scrollbar
          class="wap-content-list"
          :on-reach-bottom="handleReachBottom"
          :distance-to-edge="23"
        >
          <div
            v-for="(item, index) in shopsData"
            :key="index"
            class="wap-content-item"
            :class="{ active:selected == index }"
            @click="clickShop(item,index)"
          >
            <div>
              <img
                class="shop-logo"
                :src="item.storeLogo"
                alt=""
              />
            </div>
            <div class="wap-content-desc">
              <div class="wap-content-desc-title">{{ item.storeName }}</div>

              <div
                class="self-operated"
                :class="{'theme_color':item.selfOperated }"
              >{{ item.selfOperated ? '自营' : '非自营' }}</div>
              <div
                class="wap-sku"
                :class="{'theme_color':(item.storeDisable === 'OPEN' ? true : false) }"
              >{{ item.storeDisable === 'OPEN' ? '开启中' : '未开启' }}</div>
            </div>
          </div>
          <i
            v-if="loading"
            size="large"
            fix
          ></i>
        </el-scrollbar>
      </div>
    </div>
  </div>
</template>
<script>
import { getShopListData } from '@/api/system/store';
export default {
  data() {
    return {
      loading: false, // 加载状态
      total: '', // 总数
      params: {
        // 请求参数
        current: 1,
        size: 10,
        storeDisable: 'OPEN',
        storeName: '',
      },
      shopsData: [], // 店铺数据
      selected: 9999999999, //设置一个不可能选中的index
    };
  },
  watch: {},

  created() {
    this.init();
  },
  methods: {
    handleReachBottom() {
      setTimeout(() => {
        if (this.params.current * this.params.size <= this.total) {
          this.params.current++;
          this.init();
        }
      }, 1500);
    },
    init() {
      this.loading = true;
      getShopListData(this.params).then((res) => {
        if (res.success) {
          /**
           * 解决数据请求中，滚动栏会一直上下跳动
           */
          this.total = res.result.total;

          this.shopsData.push(...res.result.records);

          this.loading = false;
        }
      });
    },
    clickShop(val, i) {
      this.selected = i;
      val = { ...val, ___type: 'shops' };
      this.$emit('selected', [val]);
    },
  },
};
</script>
<style lang="scss" scoped>
@import '../style.scss';
.shop {
  display: flex;
}
.self-operated {
  font-size: 12px;
  color: #999;
}
.wap-content-list {
  flex-wrap: wrap;
}
.shop-logo {
  object-fit: cover;
}
.active {
  background: url('../../../assets/selected.png') no-repeat;
  background-position: right;
  background-size: 10%;
}
</style>
